package com.thingcom.delinrobot.main.Launch.Login;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.gizwits.gizwifisdk.enumration.GizUserAccountType;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiSDKListener;
import com.thingcom.delinrobot.Common.Constant;
import com.thingcom.delinrobot.R;
import com.thingcom.delinrobot.base.BaseBackFragment;
import com.thingcom.delinrobot.utils.MyLog;
import com.thingcom.delinrobot.utils.PreferencesUtil;
import com.thingcom.delinrobot.utils.ToastUtil;
import com.thingcom.delinrobot.view.SimpleToolbar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/12/4
 */
public class LoginSendEmailFragment extends BaseBackFragment {


    @BindView(R.id.btn_forget_password_ok)
    Button btnForgetPasswordOk;
    @BindView(R.id.toolbar_forget_password_two)
    SimpleToolbar toolbarForgetPasswordTwo;
    @BindView(R.id.tv_mail_no_address)
    TextView tvMailNoAddress;
    private String mail;

    public static LoginSendEmailFragment newInstance() {
        Bundle args = new Bundle();
        LoginSendEmailFragment fragment = new LoginSendEmailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_back_forget_password_two, container, false);
        unbinder = ButterKnife.bind(this, view);
        initToolbar(toolbarForgetPasswordTwo);
        mail = PreferencesUtil.getInstance(getContext()).getString(Constant.MAIL_NUMBER, null);
        GizWifiSDK.sharedInstance().setListener(mListener);
        GizWifiSDK.sharedInstance().resetPassword(mail, null, null, GizUserAccountType.GizUserEmail);

        return view;
    }


    private GizWifiSDKListener mListener = new GizWifiSDKListener() {
        @Override
        public void didChangeUserPassword(GizWifiErrorCode result) {
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
                ToastUtil.showToast(getContext(), getString(R.string.success_toast));
            } else {
                ToastUtil.showToast(getContext(), getString(R.string.fail_toast));
                MyLog.i("重置密码失败", "result"+result);
                //重置密码邮件发送失败，弹出错误信息
            }
        }
    };


    @OnClick({R.id.tv_mail_no_address, R.id.btn_forget_password_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_mail_no_address:
                GizWifiSDK.sharedInstance().resetPassword(mail, null, null, GizUserAccountType.GizUserEmail);
                break;
            case R.id.btn_forget_password_ok:
                start(LoginMailFragment.newInstance(), 2);
                break;
        }
    }
}
